package edu.yctc.weblog.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.dao.AdminDAO;
import edu.yctc.weblog.entity.AdminDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.AdminService;
import edu.yctc.weblog.utils.SHA256Util;

@Service("adminService")
public class AdminServiceImpl implements AdminService {

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Resource
    private AdminDAO adminDAO;

    @Override
    public ResultDO<AdminDO> signIn(AdminDO adminDO) {
        if (StringUtils.isBlank(adminDO.getEmail()) && StringUtils.isBlank(adminDO.getMobile())
            || StringUtils.isBlank(adminDO.getPassword())) {
            LOG.error("sign in fail, parameter invalid, adminDO={}", adminDO);
            return new ResultDO<AdminDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        AdminDO toGet = null;
        // 邮箱登陆
        if (StringUtils.isBlank(adminDO.getEmail()) == false) {
            toGet = adminDAO.getAdminDOByEmailAndPassword(adminDO.getEmail(), SHA256Util.SHA256(adminDO.getPassword()));

        } else if (StringUtils.isBlank(adminDO.getMobile()) == false) { // 手机登陆
            toGet =
                adminDAO.getAdminDOByMobileAndPassword(adminDO.getMobile(), SHA256Util.SHA256(adminDO.getPassword()));

        }
        if (toGet == null) {
            // 登陆失败
            LOG.error("sign in fail, admin not exist, adminDO={}", adminDO);
            return new ResultDO<AdminDO>(false, ResultCode.ERROR_ACCOUNT_OR_PASSWORD,
                ResultCode.MSG_ERROR_ACCOUNT_OR_PASSWORD, null);
        }
        LOG.info("sign in success, adminDO={}", toGet);
        return new ResultDO<AdminDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, toGet);
    }

    @Override
    public ResultDO<AdminDO> getAdminDOById(Long id) {
        if (id <= 0) {
            LOG.error("get admindo by id fail, parameter invalid, id={}", id);
            return new ResultDO<AdminDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        AdminDO adminDO = adminDAO.getAdminDOById(id);
        if (adminDO == null) {
            LOG.error("get admindo by id fail, admindo is null, id={}", id);
            return new ResultDO<AdminDO>(false, ResultCode.DB_ERROR, ResultCode.MSG_DB_ERROR, null);
        }
        LOG.info("get admindo by id success, id={}, admindo={}", id, adminDO);
        return new ResultDO<AdminDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, adminDO);
    }

    @Override
    @Transactional
    public ResultDO<Void> updatePassword(Long id, String originalPassword, String newPassword) {
        if (id <= 0 || StringUtils.isBlank(originalPassword) || StringUtils.isBlank(newPassword)) {
            LOG.error("update password fail, parameter invalid, id={}, original password={}, new password={}", id,
                originalPassword, newPassword);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        AdminDO adminDO = adminDAO.getAdminDOById(id);
        if (adminDO == null) {
            LOG.error("get admindo fail, admindo is null, id={}", id);
            return new ResultDO<>(false, ResultCode.ADMIN_NOT_EXIST, ResultCode.MSG_ADMIN_NOT_EXIST);
        }
        if (SHA256Util.SHA256(originalPassword).equals(adminDO.getPassword()) == false) {
            // 原密码不匹配
            LOG.error("update password fail, original password is not match, admindo={}, input original password={}",
                adminDO, originalPassword);
            return new ResultDO<>(false, ResultCode.ADMIN_ORIGINAL_PASSWORD_NOT_MATCH,
                ResultCode.MSG_ADMIN_ORIGINAL_PASSWORD_NOT_MATCH);
        }
        // 修改密码
        adminDO.setPassword(SHA256Util.SHA256(newPassword));
        adminDAO.update(adminDO);
        LOG.info("update password success, id={}, new password={}", id, newPassword);
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

    @Override
    @Transactional
    public ResultDO<Void> updateName(Long id, String name, String nick) {
        if (id <= 0 || StringUtils.isBlank(name) || StringUtils.isBlank(nick)) {
            LOG.error("update name fail, parameter invalid, name={}, nick={}", name, nick);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        AdminDO adminDO = adminDAO.getAdminDOById(id);
        if (adminDO == null) {
            LOG.error("get admindo fail, admindo is null, id={}", id);
            return new ResultDO<>(false, ResultCode.ADMIN_NOT_EXIST, ResultCode.MSG_ADMIN_NOT_EXIST);
        }
        adminDO.setName(name);
        adminDO.setNick(nick);
        adminDAO.update(adminDO);
        LOG.info("update name success, id={}, name={}, nick={}", id, name, nick);
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

}
